from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from App.models import *


# Create your views here.


def index(request):
    return render(request, 'index.html')


def user_list(request):
    # 获取所有用户数据
    users = Username.objects.all()
    return render(request, 'user_list.html', {'users': users})


def user_detail(request, uid):
    users = Username.objects.get(pk=uid)
    return render(request, 'user_detail.html', {'users': users})


# 多个参数
def user_ab_view(request, a, b):
    return HttpResponse(f'a:{a} - b:{b}')


# 多个参数
# 要和路由中的参数名一致。与顺序无关
def user_ba_view(request, b, a):
    return HttpResponse(f'a:{a} - b:{b}')


# 在视图函数中做跳转
def my_redicret(request):
    # return redirect('http://www.baidu.com')
    # return redirect('/user/index/')
    # return redirect('/user/userdetail/2')

    # 反向解析
    # reverse('App:userdetail', args=(1,)) ==>>  userdetail/<int:uid>

    # 带命名空间
    # return redirect(reverse('App:userdetail', args=(1,)))  # 位置传参
    return redirect(reverse('App:userdetail', kwargs={'uid': 2}))  # 关键字传参

    # 不带命名空间
    # return redirect(reverse('userdetail', kwargs={'uid': 2}))  # 报错  NoReverseMatch at /user/myredicret/


